package com.wxl.common.util;

import android.Manifest;
import android.app.Activity;
import android.app.AlarmManager;
import android.content.ContentResolver;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.SystemClock;
import android.provider.Settings;
import android.telephony.TelephonyManager;

import androidx.core.content.ContextCompat;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

/**
 * 系统工具类
 * Created by zhuwentao on 2016-07-18.
 */
public class SystemUtil {

    /**
     * 获取当前手机系统语言。
     *
     * @return 返回当前系统语言。例如：当前设置的是“中文-中国”，则返回“zh-CN”
     */
    public static String getSystemLanguage() {
        return Locale.getDefault().getLanguage();
    }

    /**
     * 获取当前系统上的语言列表(Locale列表)
     *
     * @return  语言列表
     */
    public static Locale[] getSystemLanguageList() {
        return Locale.getAvailableLocales();
    }

    /**
     * 获取当前手机系统版本号
     *
     * @return  系统版本号
     */
    public static String getSystemVersion() {
        return android.os.Build.VERSION.RELEASE;
    }

    /**
     * 获取手机型号
     *
     * @return  手机型号
     */
    public static String getSystemModel() {
        return android.os.Build.MODEL;
    }

    /**
     * 获取手机厂商
     *
     * @return  手机厂商
     */
    public static String getDeviceBrand() {
        return android.os.Build.BRAND;
    }

    /**
     * 获取手机IMEI(需要“android.permission.READ_PHONE_STATE”权限)
     *
     * @return  手机IMEI
     */
    public static String getIMEI(Context ctx) {
        TelephonyManager tm = (TelephonyManager) ctx.getSystemService(Activity.TELEPHONY_SERVICE);
        if (tm != null) {
            return tm.getDeviceId();
        }
        return null;
    }

    public static String getMac() {
        String macSerial = null;
        String str = "";

        try
        {
            Process pp = Runtime.getRuntime().exec("cat /sys/class/net/wlan0/address ");
            InputStreamReader ir = new InputStreamReader(pp.getInputStream());
            LineNumberReader input = new LineNumberReader(ir);

            for (; null != str;)
            {
                str = input.readLine();
                if (str != null)
                {
                    macSerial = str.trim();// 去空格
                    break;
                }
            }
        } catch (IOException ex) {
            // 赋予默认值
            ex.printStackTrace();
        }
        return macSerial;
    }

    /**
     * 获取设备号
     *
     * @return
     */
    public static String getDivicesNumbr(Context context) {
        if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
            Toll.Companion.toll("没有权限");
            return "";
        }
        TelephonyManager
                telephonyManager =
                (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        //手机串号:GSM手机的IMEI 和 CDMA手机的 MEID.
        String deviceID = telephonyManager.getDeviceId();

        return deviceID;
    }

    /**
     * 判断系统是否自动获取时区
     */
    public static boolean isTimeZoneAuto(Context mContext) {
        try {
            return Settings.Global.getInt(mContext.getContentResolver(),
                    Settings.Global.AUTO_TIME_ZONE) > 0;
        } catch (Settings.SettingNotFoundException e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 设置自动获取时区
     */
    public static void setAutoTimeZone(Context mContext, int checked) {
        Settings.Global.putInt(mContext.getContentResolver(),
                Settings.Global.AUTO_TIME_ZONE, checked);
    }

    /**
     * 获取系统默认时区
     */
    public static String getTimeZone() {
        return TimeZone.getDefault().getDisplayName(false, TimeZone.SHORT);
    }

    /**
     * 设置系统默认时区
     */
    // 需要添加权限<uses-permission android:name="android.permission.SET_TIME_ZONE" />
    public static void setChinaTimeZone(Context context) {
       /* TimeZone.getTimeZone("GMT+8");
        TimeZone.setDefault(TimeZone.getTimeZone("GMT+8"));*/
        AlarmManager mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        mAlarmManager.setTimeZone("Asia/Shanghai");// Asia/Taipei//GMT+08:00
    }

    /**
     * 判断系统是否自动获取日期和时间
     */
    public static boolean isTimeAuto(Context mContext) {
        try {
            return Settings.Global.getInt(mContext.getContentResolver(),
                    Settings.Global.AUTO_TIME) > 0;
        } catch (Settings.SettingNotFoundException e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 设置系统自动获取日期和时间
     */
    public static void setAutoDateTime(Context mContext, int checked) {
        Settings.Global.putInt(mContext.getContentResolver(),
                Settings.Global.AUTO_TIME, checked);
    }

    /**
     * 获取系统当前时间
     */
    public static String currentTime() {
        long currentTime = System.currentTimeMillis();
        Date date = new Date(currentTime);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        return formatter.format(date);
    }

    /**
     * 设置系统当前时间
     */
    public static void setSytemTime(long value){
        long settingTime = value;
        if (value/1000 < Integer.MAX_VALUE){
            SystemClock.setCurrentTimeMillis(settingTime);
        }
    }

    /**
     * 判断系统是否是24h格式
     */
    public static boolean is24Hour(Context mContext) {
        ContentResolver cv = mContext.getContentResolver();
        String strTimeFormat = Settings.System.getString(cv,
                Settings.System.TIME_12_24);
        if (strTimeFormat != null && strTimeFormat.equals("24")) {
            return true;
        }
        return false;
    }

    /**
     * 设置系统是否是24h格式
     */
    public static void set24Hour(Context mContext) {
        ContentResolver cv = mContext.getContentResolver();
        Settings.System.putString(cv, Settings.System.TIME_12_24, "24");
    }

    /**
     * 判断系统是否是12h格式
     */
    public static boolean is12Hour(Context mContext) {
        ContentResolver cv = mContext.getContentResolver();
        String strTimeFormat = Settings.System.getString(cv,
                Settings.System.TIME_12_24);
        if (strTimeFormat != null && strTimeFormat.equals("12")) {
            return true;
        }
        return false;
    }

    /**
     * 设置系统是否是12h格式
     */
    public static void set12Hour(Context mContext) {
        ContentResolver cv = mContext.getContentResolver();
        Settings.System.putString(cv, Settings.System.TIME_12_24, "12");
    }


    /**
     * 获取当前的versioncode
     * @param context
     * @return
     */
    public static String getVersionName(Context context) {
        PackageInfo pi = null;
        try {
            PackageManager pm = context.getPackageManager();//context为当前Activity上下文
            pi = pm.getPackageInfo(context.getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return pi.versionName;
    }

}

